# Clear workspace
rm(list = ls())

library(tidyverse)
library(lfe)

# Set default repository
setwd(dirname(rstudioapi::getSourceEditorContext()$path))

# Import data ####
df = read_csv("final_data.csv") 

# Set control variables
ind_cov = cbind(df$female, df$local, df$foregin, df$anonymous, df$distance_km)
pol_cov = cbind(df$national_twosession, df$city_twosession, df$local_turnover)
covariate = cbind(ind_cov, pol_cov)

# Table 1: OLS Estimation #### 
m1 = felm(res_speed ~ price_l | v_class1 + yearmonth | 0 | apt_name, df) 
m2 = felm(res_speed ~ price_l + ind_cov | v_class1 + yearmonth | 0 | apt_name, df) 
m3 = felm(res_speed ~ price_l + ind_cov + pol_cov | v_class1 + yearmonth | 0 | apt_name, df) 

m4 = felm(resolve_0 ~ price_l | v_class1 + yearmonth | 0 | apt_name, df) 
m5 = felm(resolve_0 ~ price_l + ind_cov | v_class1 + yearmonth | 0 | apt_name, df) 
m6 = felm(resolve_0 ~ price_l + ind_cov + pol_cov | v_class1 + yearmonth | 0 | apt_name, df) 

stargazer::stargazer(m1, m2, m3, m4, m5, m6,
                     title = "Main Result",
                     no.space = TRUE,
                     type = "text", 
                     style = "qje",
                     label = "ols_result",
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     covariate.labels = c("Price", "Female", "Local",
                                          "Foreign", "Anonymous", "Distance to Neighborhood Center", 
                                          "National Two Sessions", "Local Two Sessions", "Executive Turnover"),
                     dep.var.labels = c("Resolution Time", "Positive Resolution"),
                     add.lines = list(c("Type FE", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Year-month FE", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"))
)


# Table 2: 2SLS Estimation #### 
m1 = felm(price_l ~ treatment | v_class1 + yearmonth | 0 | apt_name, df)
f_stats_m1 = summary(m1)$P.fstat[5] %>% round(2)

m2 = felm(res_speed ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df) 

m3 = felm(res_speed ~ treatment + covariate | v_class1 + yearmonth | 0 | apt_name, df) 

m4 = felm(resolve_0 ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df) 
m5 = felm(resolve_0 ~ treatment + covariate | v_class1 + yearmonth | 0 | apt_name, df) 

stargazer::stargazer(m1, m2, m3, m4, m5,
                     type = "text",
                     style = "qje",
                     no.space = TRUE,
                     label = "iv_result",
                     title = "2SLS Estimation",
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     keep = c("price_l", "treatment"),
                     column.labels = c("First Stage", "IV", "RF", "IV", "RF"),
                     notes = "",
                     covariate.labels = c("Settlement", "Price"),
                     dep.var.labels = c("Price", "Resolution Time", "Positive Resolution"),
                     add.lines = list(c("F Statistics", as.character(f_stats_m1)),
                                      c("Type FE", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Year-month FE", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Controls", "N", "Y", "Y", "Y", "Y", "Y"))
)


# Table 3: Alternative Mechanism #### 
m1 = felm(law ~ price_l + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m2 = felm(law ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df) 

m3 = felm(shangfang ~ price_l + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m4 = felm(shangfang ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df) 

m5 = felm(upper ~ price_l + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m6 = felm(upper ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df) 

m7 = felm(party_member ~ price_l + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m8 = felm(party_member ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df) 

stargazer::stargazer(m1, m2, m3, m4,
                     m5, m6, m7, m8,
                     style = "qje",
                     type = "text",
                     label = "language_full_model",
                     keep = c("price_l", "price_l(fit)"),
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     column.labels = c(rep(c("OLS", "IV"), 4)),
                     covariate.labels = c("Price", "Price"),
                     dep.var.labels = c("Legal", "Protest", "Upper Gov", "CCP Member"),
                     add.lines = list(c("Type FE", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Year-month FE", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Controls", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"))
)


# Table 4: Alternative Explanation - Local Funding #### 
m1 = felm(price_l ~ log(fiscal_rev_per) | v_class1 + yearmonth | 0 | apt_name, df) 
m2 = felm(price_l ~ log(fiscal_exp_per) | v_class1 + yearmonth | 0 | apt_name, df) 
m3 = felm(res_speed ~ log(fiscal_rev_per) + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m4 = felm(res_speed ~ log(fiscal_exp_per) + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m5 = felm(resolve_0 ~ log(fiscal_rev_per) + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m6 = felm(resolve_0 ~ log(fiscal_exp_per) + covariate | v_class1 + yearmonth | 0 | apt_name, df) 

stargazer::stargazer(m1, m2, m3, m4, m5, m6, title = "Public Expenditure Analysis",
                     no.space = TRUE,
                     type = "text", 
                     style = "qje",
                     label = "expenditure_analysis",
                     keep = c("price_l", "fiscal_exp_per", "fiscal_rev_per"),
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     covariate.labels = c("District Govt. Transfer", "Expenditure"),
                     dep.var.labels = c("Price", "Resolution Time", "Positive Resolution"),
                     add.lines = list(c("Type FE", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Year-month FE", "Y", "Y", "Y", "Y", "Y"),
                                      c("Controls", "N", "N", "Y", "Y", "Y", "Y"))
)


# Table 5: Housing Management Quality ####
apt_w_hoa = df %>% filter(!is.na(hoa)) %>% distinct(apt_name)  # 331 apartments with HOA data

apt_total = unique(df$apt_name) %>% length() # 470 total apartments

331 / 470 # Ratio of apartments with HOA fee data

m1 = felm(res_speed ~ hoa + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m2 = felm(res_speed ~ price_l + hoa + covariate | v_class1 + yearmonth | 0 | apt_name, df) 

m3 = felm(resolve_0 ~ hoa + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m4 = felm(resolve_0 ~ price_l + hoa + covariate | v_class1 + yearmonth | 0 | apt_name, df) 

stargazer::stargazer(m1, m2, m3, m4,
                     title = "Housing Management Quality",
                     no.space = TRUE,
                     type = "text", 
                     style = "qje",
                     label = "hoa",
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     keep = c("price_l", "hoa"),
                     covariate.labels = c("Price", "Property Management Fee"),
                     dep.var.labels = c("Resolution Time", "Positive Resolution"),
                     add.lines = list(
                       c("Baseline Controls", "Y", "Y", "Y", "Y"),
                       c("Type FE", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
                       c("Year-month FE", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"))
)


################################# Supplementary Material #########################################

# Table D1: Satisfaction Analysis ####
m1 = felm(statisfaction ~ resolve_0 | v_class1 + yearmonth | 0 | apt_name, df) 
m2 = felm(statisfaction ~ res_speed | v_class1 + yearmonth | 0 | apt_name, df) 
m3 = felm(statisfaction ~ res_speed + resolve_0 | v_class1 + yearmonth | 0 | apt_name, df) 
m4 = felm(statisfaction ~ res_speed + resolve_0 + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m5 = felm(statisfaction ~ resolve_0 * res_speed | v_class1 + yearmonth | 0 | apt_name, df) 
m6 = felm(statisfaction ~ resolve_0 * res_speed + covariate | v_class1 + yearmonth | 0 | apt_name, df) 

stargazer::stargazer(m1, m2, m3, m4, m5, m6,
                     title = "Effect of Responsiveness on Satisfaction",
                     no.space = TRUE,
                     type = "text", 
                     style = "qje",
                     label = "statisfaction",
                     keep = c("res_speed", "resolve_0", "resolve_0:res_speed"),
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     covariate.labels = c("Positive Resolution", "Resolution Time",
                                          "Resolution Time * Positive Resolution"),
                     dep.var.labels = c("Satisfaction"),
                     add.lines = list(c("Type FE", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Year-month FE", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Controls", "N", "N", "N", "Y", "N", "Y"))
)


# Table F1: Frequency of Channels to the 12345 Hotline #### 
questionr::freq(df$v_source, sort = "dec") %>% 
  xtable::xtable(format = "latex",
                 label = "source_freq",
                 caption = "Frequency of Channels to the 12345 Hotline")


# Table F2: Summary Statistics #### 
df %>% 
  dplyr::select(res_speed, resolve_0, statisfaction, price,
                female, local, foregin, anonymous, distance_km,
                national_twosession, city_twosession,
                local_turnover) %>% 
  as.data.frame() %>% 
  stargazer::stargazer(
    digits = 2,
    type = "text",
    covariate.labels = c("Resolution Time", "Positive Resolution", "Satisfaction",
                         "Price (RMB per square meter)",
                         "Female", "Local", "Foreign", "Anonymous",
                         "Distance to Neighborhood Center (km)",
                         "National Two Sessions", "Local Two Sessions",
                         "Executive Turnover"),
    title = "Summary Statistics",
    label = "summary_stats"
  ) 


# Table F3: Ordinal Measure #### 
m1 = felm(res_speed_ord ~ price_l + covariate | v_class1 + yearmonth | 0 | apt_name, df)
m2 = felm(res_speed_ord ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df)
m3 = felm(res_speed_ord ~ treatment + covariate | v_class1 + yearmonth | 0 | apt_name, df)

m4 = felm(resolve_cat ~ price_l + covariate | v_class1 + yearmonth | 0 | apt_name, df)  
m5 = felm(resolve_cat ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df)  
m6 = felm(resolve_cat ~ treatment + covariate | v_class1 + yearmonth | 0 | apt_name, df)  

stargazer::stargazer(m1, m2, m3, m4, m5, m6,
                     title = "Ordinal Measure of Outcome Variable",
                     no.space = TRUE,
                     type = "text", 
                     style = "qje",
                     label = "ordinal",
                     column.labels = c("OLS", "IV", "RF", "OLS", "IV", "RF"),
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     keep = c("price_l", "treatment"),
                     covariate.labels = c("Price", "Settlement", "Price"),
                     dep.var.labels = c("Resolution Time (Ordinal)", "Positive Resolution (Ordinal)"),
                     add.lines = list(c("Type FE", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Year-month FE", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Controls", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"))
)


# Table F4: Correlation Plot between Resolution Decision and Time #### 
m1 = felm(resolve_0 ~ res_speed | 0 | 0 | apt_name, df) 
m2 = felm(resolve_0 ~ res_speed | v_class1 | 0 | apt_name, df) 
m3 = felm(resolve_0 ~ res_speed | v_class1 + yearmonth | 0 | apt_name, df) 

stargazer::stargazer(m1, m2, m3,
                     title = "Correlational Analysis",
                     no.space = TRUE,
                     type = "text", 
                     style = "qje",
                     omit = ("Constant"),
                     label = "cor_time_resolution",
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     covariate.labels = c("Resolution Time"),
                     dep.var.labels = c("Positive Resolution"),
                     add.lines = list(c("Type FE", "N", "Y", "Y"),
                                      c("Year-month FE", "N", "N", "Y"))
)


# Table F5: Exclusion Restriction Analysis  #### 
m1 = felm(female ~ treatment | v_class1 + yearmonth | 0 | neighborhood, df) 
m2 = felm(local ~ treatment | v_class1 + yearmonth | 0 | neighborhood, df) 
m3 = felm(anonymous ~ treatment | v_class1 + yearmonth | 0 | neighborhood, df) 
m4 = felm(foregin ~ treatment | v_class1 + yearmonth | 0 | neighborhood, df) 
m5 = felm(distance_km ~ treatment | v_class1 + yearmonth | 0 | neighborhood, df) 

stargazer::stargazer(m1, m2, m3, m4, m5,
                     title = "Exclusion Restriction",
                     no.space = TRUE,
                     type = "text", 
                     style = "qje",
                     label = "exclusion",
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     dep.var.labels = c("Female", "Local", "Anonymous", "Foreign", "Distance to Neighborhood Center"),
                     covariate.labels = c("Settlement"),
                     add.lines = list(c("Type FE", "Y", "Y", "Y", "Y", "Y"),
                                      c("Year-month FE", "Y", "Y", "Y", "Y", "Y"))
)


# Table F6: Phone Call Only #### 
m1 = felm(res_speed ~ price_l + covariate | v_class1 + yearmonth | 0 | apt_name, df, subset = (v_source == "Phone"))  
m2 = felm(res_speed ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df, subset = (v_source == "Phone"))  
m3 = felm(res_speed ~ treatment + covariate | v_class1 + yearmonth | 0 | apt_name, df, subset = (v_source == "Phone"))  

m4 = felm(resolve_0 ~ price_l + covariate | v_class1 + yearmonth | 0 | apt_name, df, subset = (v_source == "Phone"))  
m5 = felm(resolve_0 ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df, subset = (v_source == "Phone"))  
m6 = felm(resolve_0 ~ treatment + covariate | v_class1 + yearmonth | 0 | apt_name, df, subset = (v_source == "Phone"))  

stargazer::stargazer(m1, m2, m3, m4, m5, m6,
                     title = "Analysis Using Phone Call Only Sample",
                     no.space = TRUE,
                     type = "text", 
                     style = "qje",
                     label = "phone_only",
                     column.labels = c("OLS", "IV", "RF", "OLS", "IV", "RF"),
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     keep = c("price_l", "treatment"),
                     covariate.labels = c("Price", "Settlement", "Price"),
                     dep.var.labels = c("Resolution Time", "Positive Resolution"),
                     add.lines = list(c("Type FE", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Year-month FE", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Controls", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"))
)


# Table F7: Using Alternative Measure of Resolution ####

m1 = felm(res_speed0 ~ price_l + ind_cov + pol_cov | v_class1 + yearmonth | 0 | apt_name, df) 
m2 = felm(res_speed0 ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df) 

m3 = felm(res_duration ~ price_l + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m4 = felm(res_duration ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df) 

m5 = felm(close_duration ~ price_l + covariate | v_class1 + yearmonth | 0 | apt_name, df) 
m6 = felm(close_duration ~ 0 + covariate | v_class1 + yearmonth | (price_l ~ treatment) | apt_name, df) 

stargazer::stargazer(m1, m2, m3, m4, m5, m6,
                     style = "qje",
                     digits = 3,
                     type = "text",
                     no.space = TRUE,
                     label = "alter_measure",
                     keep = c("price_l", "price_l(fit)"),
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     column.labels = c(rep(c("OLS", "IV"), 3)),
                     covariate.labels = c("Price", "Price"),
                     dep.var.labels = c("Late Resolution (Binary)", "Allocation Time", "Precise Resolution Time (Aug 18- July 19)"),
                     add.lines = list(c("Type FE", "Y", "Y", "Y", "Y", "Y", "Y"),
                                      c("Year-month FE", "Y", "Y", "Y", "Y", "Y"),
                                      c("Controls", "Y", "Y", "Y", "Y", "Y", "Y"))
)


# Table F8: Spatial-adjusted Standard Errors####
# Check Stata do file spatial_analysis.do


# Table F9: Verbal Signal Summary Statistics ####
df %>% 
  select(law, shangfang, upper, party_member) %>% 
  as.data.frame() %>% 
  stargazer::stargazer(
    type = "text",
    label = "sum_stats_alter",
    title = "Summary Statistics of Verbal Signals",
    covariate.labels = c("Legal", "Collective Action",
                         "Upper Government", "CCP Member")
  )


# Table F10: Correlation Between Housing Price and Petition Frequency ####
frequency = read_rds("frequency.rds")
m0 = felm(n ~ price_l, frequency)
m1 = felm(n_l ~ price_l, frequency)
m2 = felm(n_pc ~ price_l, frequency)
m3 = felm(n_pc_log ~ price_l, frequency)

stargazer::stargazer(m0, m1, m2, m3,
                     style = "qje",
                     title = "Correlation Between Housing Price and Petition Frequency",
                     no.space = TRUE,
                     type = "text",
                     label = "demand_side",
                     keep = c("price_l"),
                     column.labels = c(rep(c("raw", "logged"), 2)),
                     omit.stat = c("LL", "ser", "f", "rsq"),
                     covariate.labels = c("Price"),
                     dep.var.labels = c("Petitions", "Logged Petitions", "Petitions Per Capita", "Logged Petitions Per Capita")
)

